using Coding4Fun.Phone.Controls;
using MicroMsg.Common.Event;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Shell;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.Page
{
	public class EditCircleContentPage : BasePage
	{
		public delegate void DelegateComment(string strComment);
		private enum InputMode
		{
			InputTxtMode,
			InputSmileyMode,
			NoneMode
		}
		private EditCircleContentPage.DelegateComment mCommentCallBack;
		private bool bIsFirstLoad = true;
		private bool bManualUnfocusCallBack;
		private EditCircleContentPage.InputMode nowMode = EditCircleContentPage.InputMode.NoneMode;
		private EventWatcher mWatchShare;
		private ApplicationBar mBar;
		private ApplicationBarIconButton OkMenuBtn;
		private ApplicationBarIconButton smelyBtn;
		internal Grid LayoutRoot;
		internal StackPanel panel;
		internal TextBlock txtPageTitle;
		internal MicroMsg.UI.UserContrl.DynamicTextBlock txtTile;
		internal MMHttpImg image;
		internal MicroMsg.UI.UserContrl.DynamicTextBlock txtContent;
		internal ChatInputTextBox InputTextBox;
		internal SmileyOverlay SmileyView;
		private bool _contentLoaded;
		public EditCircleContentPage()
		{
			this.InitializeComponent();
			this.image.imgContent.set_Stretch(3);
			base.add_Loaded(new RoutedEventHandler(this.EditCircleContentPage_Loaded));
		}
		private void EditCircleContentPage_Loaded(object sender, RoutedEventArgs e)
		{
			if (this.bIsFirstLoad)
			{
				this.InitResource();
				this.InitSmiley();
				base.set_ApplicationBar(this.GetApplicationBar());
				this.InputTextBox.add_Tap(new EventHandler<GestureEventArgs>(this.InputTextBox_Tap));
				if (this.InputTextBox != null && this.InputTextBox.realBox != null)
				{
					this.InputTextBox.realBox.add_LostFocus(new RoutedEventHandler(this.InputTextBox_LostFocus));
				}
				this.bIsFirstLoad = false;
			}
		}
		private void InputTextBox_LostFocus(object sender, RoutedEventArgs e)
		{
			if (this.bManualUnfocusCallBack)
			{
				this.bManualUnfocusCallBack = false;
				return;
			}
			if (this.SmileyView.get_Visibility() == 1 && this.LayoutRoot.get_Margin().get_Top() < 0.0 && this.nowMode == EditCircleContentPage.InputMode.InputTxtMode)
			{
				this.LayoutRoot.set_Margin(new Thickness(0.0, 0.0, 0.0, 0.0));
				this.nowMode = EditCircleContentPage.InputMode.NoneMode;
			}
		}
		private void textBoxFocus(bool isFocus)
		{
			if (this.InputTextBox != null && this.InputTextBox.realBox != null)
			{
				try
				{
					if (isFocus)
					{
						this.InputTextBox.FocusRealBox();
					}
					else
					{
						this.bManualUnfocusCallBack = true;
						base.Focus();
					}
				}
				catch
				{
				}
			}
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 != null && 5 == object_0.Length)
			{
				if (object_0[0] != null && object_0[0] is EditCircleContentPage.DelegateComment && object_0[1] is string && object_0[2] is string && object_0[3] is string)
				{
					this.mCommentCallBack = (object_0[0] as EditCircleContentPage.DelegateComment);
					this.txtPageTitle.set_Text(object_0[1] as string);
					this.txtTile.Text = (object_0[2] as string);
					this.txtContent.Text = (object_0[3] as string);
				}
				if (object_0[4] is BitmapImage)
				{
					this.image.set_Visibility(0);
					this.image.imgContent.set_Source(object_0[4] as BitmapImage);
				}
				else
				{
					if (object_0[4] is string)
					{
						string text = object_0[4] as string;
						if (string.IsNullOrWhiteSpace(text))
						{
							this.image.set_Visibility(1);
							this.txtContent.set_MaxWidth(440.0);
						}
						else
						{
							this.image.set_Visibility(0);
							this.image.SetUrl(text);
						}
					}
					else
					{
						this.image.set_Visibility(1);
						this.txtContent.set_MaxWidth(440.0);
					}
				}
			}
			this.InputTextBox.HintText = strings.InputBox_ShareToSns_Hint;
			this.registerEvent();
		}
		protected override void OnPageOut()
		{
			this.unRegisterEvent();
		}
		private void InitResource()
		{
			this.LayoutRoot.set_Background(R.theme.SNS_MAINPAGE_BK_BRUSH);
		}
		private void SwitchToMode(EditCircleContentPage.InputMode mode)
		{
			if (mode == EditCircleContentPage.InputMode.InputSmileyMode)
			{
				this.LayoutRoot.set_Margin(new Thickness(0.0, -408.0, 0.0, 0.0));
				base.Focus();
				base.get_Dispatcher().BeginInvoke(delegate
				{
					this.SmileyView.set_Visibility(0);
				});
				this.nowMode = EditCircleContentPage.InputMode.InputSmileyMode;
			}
			else
			{
				if (mode == EditCircleContentPage.InputMode.InputTxtMode)
				{
					this.LayoutRoot.set_Margin(new Thickness(0.0, -408.0, 0.0, 0.0));
					this.SmileyView.set_Visibility(1);
					this.nowMode = EditCircleContentPage.InputMode.InputTxtMode;
				}
				else
				{
					if (mode == EditCircleContentPage.InputMode.NoneMode)
					{
						this.LayoutRoot.set_Margin(new Thickness(0.0, 0.0, 0.0, 0.0));
						this.SmileyView.set_Visibility(1);
						base.Focus();
						this.nowMode = EditCircleContentPage.InputMode.NoneMode;
					}
				}
			}
			this.RefreshAppBar();
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			if (this.nowMode == EditCircleContentPage.InputMode.InputSmileyMode || this.nowMode == EditCircleContentPage.InputMode.InputTxtMode)
			{
				this.panel.set_Margin(new Thickness(0.0, 32.0, 0.0, 0.0));
				this.SwitchToMode(EditCircleContentPage.InputMode.NoneMode);
				cancelEventArgs_0.set_Cancel(true);
			}
		}
		private void InitSmiley()
		{
			this.SmileyView.Click = new SmileyClick(this.SmileyClick);
			this.SmileyView.EmojiClick = new SmileyClick(this.EmojiClick);
		}
		private void SmileyClick(int smiley)
		{
			if (smiley == -1)
			{
				this.panel.set_Margin(new Thickness(0.0, 32.0, 0.0, 0.0));
				this.SwitchToMode(EditCircleContentPage.InputMode.NoneMode);
				return;
			}
			if (smiley != -2)
			{
				int selectionStart = this.InputTextBox.realBox.get_SelectionStart();
				string text = this.InputTextBox.Text;
				string text2 = text.Substring(0, selectionStart);
				string smileyText = SmileyManager.GetInstance().getSmileyText(smiley);
				this.SmileyView.PushSmileyStack(smileyText);
				string text3 = text2 + smileyText;
				int length = text3.get_Length();
				text3 += text.Substring(selectionStart);
				this.InputTextBox.Text = text3;
				this.InputTextBox.realBox.set_SelectionStart(length);
				this.InputTextBox.realBox.set_SelectionLength(0);
				return;
			}
			if (this.InputTextBox.realBox.get_SelectionLength() > 0)
			{
				int selectionStart2 = this.InputTextBox.realBox.get_SelectionStart();
				int selectionLength = this.InputTextBox.realBox.get_SelectionLength();
				string text4 = this.InputTextBox.Text;
				string text5 = text4.Substring(0, selectionStart2) + text4.Substring(selectionStart2 + selectionLength);
				this.InputTextBox.Text = text5;
				this.InputTextBox.realBox.set_SelectionStart(selectionStart2);
				this.InputTextBox.realBox.set_SelectionLength(0);
				return;
			}
			int selectionStart3 = this.InputTextBox.realBox.get_SelectionStart();
			string text6 = this.InputTextBox.Text;
			string text7 = text6.Substring(0, selectionStart3);
			string text8 = this.SmileyView.RemoveLastSmiley(text7);
			int num = text7.get_Length() - text8.get_Length();
			text7 = text8 + text6.Substring(selectionStart3);
			this.InputTextBox.Text = text7;
			this.InputTextBox.realBox.set_SelectionStart(selectionStart3 - num);
			this.InputTextBox.realBox.set_SelectionLength(0);
		}
		private void EmojiClick(int emojiIndex)
		{
			int selectionStart = this.InputTextBox.realBox.get_SelectionStart();
			string text = this.InputTextBox.Text;
			string text2 = text.Substring(0, selectionStart);
			string utf16CodeByIndex = EmojiManager.GetInstance().GetUtf16CodeByIndex(emojiIndex);
			this.SmileyView.PushSmileyStack(utf16CodeByIndex);
			string text3 = text2 + utf16CodeByIndex;
			int length = text3.get_Length();
			text3 += text.Substring(selectionStart);
			this.InputTextBox.Text = text3;
			this.InputTextBox.realBox.set_SelectionStart(length);
			this.InputTextBox.realBox.set_SelectionLength(0);
		}
		private void releaseCover()
		{
			ProcessingCover.releaseProgressBar(this.LayoutRoot);
		}
		private void getCover(string strToShow = null)
		{
			ProcessingCover.getProgressBar(this.LayoutRoot, strToShow, null, false, false, null);
		}
		private void registerEvent()
		{
			if (this.mWatchShare == null)
			{
				this.mWatchShare = new EventWatcher(this, null, new EventHandlerDelegate(this.OnShare));
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_SNS_POST_SUCCESS, this.mWatchShare);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_SNS_POST_ERR, this.mWatchShare);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchShare != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_SNS_POST_SUCCESS, this.mWatchShare);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_SNS_POST_ERR, this.mWatchShare);
				this.mWatchShare = null;
			}
		}
		private void OnShare(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			switch (evtArgs.mEventID)
			{
			case EventConst.ON_NETSCENE_SNS_POST_SUCCESS:
			{
				this.releaseCover();
				ToastPrompt toastPrompt = MessageBoxManager.ShowToast(strings.SendWeibo_Send_Suc, 1000, null, false);
				toastPrompt.Completed += new EventHandler<PopUpEventArgs<string, PopUpResult>>(this.toast_Completed);
				return;
			}
			case EventConst.ON_NETSCENE_SNS_POST_ERR:
				this.releaseCover();
				MessageBoxManager.ShowToast(strings.SendWeibo_Send_Error, 1000, null, false);
				return;
			default:
				return;
			}
		}
		private void toast_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
		{
			base.GoBack();
		}
		private void InputTextBox_Tap(object sender, RoutedEventArgs e)
		{
			this.textBoxFocus(false);
			this.SwitchToMode(EditCircleContentPage.InputMode.InputTxtMode);
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this.textBoxFocus(true);
			});
		}
		private void RefreshAppBar()
		{
			if (base.get_ApplicationBar() == null)
			{
				return;
			}
			if (this.nowMode == EditCircleContentPage.InputMode.NoneMode)
			{
				if (base.get_ApplicationBar().get_Buttons().get_Count() > 1)
				{
					base.get_ApplicationBar().get_Buttons().RemoveAt(0);
					return;
				}
			}
			else
			{
				if (base.get_ApplicationBar().get_Buttons().get_Count() == 1)
				{
					base.get_ApplicationBar().get_Buttons().Insert(0, this.smelyBtn);
				}
				if (this.nowMode == EditCircleContentPage.InputMode.InputSmileyMode)
				{
					this.smelyBtn.set_Text(strings.Album_TakingPhoto_Text);
					this.smelyBtn.set_IconUri(new Uri("/Resource/Img/Chat/keyboard.png", 2));
					return;
				}
				this.smelyBtn.set_Text(strings.Album_TakingPhoto_Smely);
				this.smelyBtn.set_IconUri(new Uri("/Resource/Img/Chat/expression.png", 2));
			}
		}
		private ApplicationBar GetApplicationBar()
		{
			if (this.mBar != null)
			{
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			if (this.mBar != null)
			{
				this.mBar.set_BackgroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_BK_BRUSH).get_Color());
				this.mBar.set_ForegroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_FG_BRUSH).get_Color());
			}
			this.OkMenuBtn = new ApplicationBarIconButton(new Uri("/icons/appbar.check.rest.png", 2));
			this.OkMenuBtn.set_Text(strings.COM_Confirm);
			this.OkMenuBtn.add_Click(new EventHandler(this.OkMenuBtn_Click));
			this.mBar.get_Buttons().Add(this.OkMenuBtn);
			this.smelyBtn = new ApplicationBarIconButton(new Uri("/icons/appbar.check.rest.png", 2));
			this.smelyBtn.set_Text(strings.Album_TakingPhoto_Smely);
			this.smelyBtn.add_Click(new EventHandler(this.smelyBtn_Click));
			return this.mBar;
		}
		private void smelyBtn_Click(object sender, EventArgs e)
		{
			if (this.nowMode != EditCircleContentPage.InputMode.NoneMode && this.nowMode != EditCircleContentPage.InputMode.InputTxtMode)
			{
				if (this.InputTextBox != null && this.InputTextBox.realBox != null)
				{
					try
					{
						this.InputTextBox.FocusRealBox();
						this.LayoutRoot.set_Margin(new Thickness(0.0, -408.0, 0.0, 0.0));
						this.SmileyView.set_Visibility(1);
						this.nowMode = EditCircleContentPage.InputMode.InputTxtMode;
						this.RefreshAppBar();
					}
					catch
					{
					}
				}
				return;
			}
			this.SwitchToMode(EditCircleContentPage.InputMode.InputSmileyMode);
		}
		private void OkMenuBtn_Click(object sender, EventArgs e)
		{
			this.panel.set_Margin(new Thickness(0.0, 32.0, 0.0, 0.0));
			this.SwitchToMode(EditCircleContentPage.InputMode.NoneMode);
			if (this.mCommentCallBack != null)
			{
				this.getCover(null);
				string text = this.InputTextBox.Text.Trim();
				if (!string.IsNullOrEmpty(text))
				{
					bool flag = false;
					bool flag2 = false;
					string text2 = EmojiManager.GetInstance().ConvertInputStrToCodeStr(text, ref flag);
					text2 = EmojiManager.GetInstance().SbCodeToUtf16Code(text2, ref flag2);
					this.InputTextBox.Text = "";
					this.SmileyView.ClearSmileyStack();
					this.mCommentCallBack(text2);
					return;
				}
				this.InputTextBox.Text = "";
				this.SmileyView.ClearSmileyStack();
				this.mCommentCallBack("");
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/TXBlogRead/UI/EditCircleContentPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.panel = (StackPanel)base.FindName("panel");
			this.txtPageTitle = (TextBlock)base.FindName("txtPageTitle");
			this.txtTile = (MicroMsg.UI.UserContrl.DynamicTextBlock)base.FindName("txtTile");
			this.image = (MMHttpImg)base.FindName("image");
			this.txtContent = (MicroMsg.UI.UserContrl.DynamicTextBlock)base.FindName("txtContent");
			this.InputTextBox = (ChatInputTextBox)base.FindName("InputTextBox");
			this.SmileyView = (SmileyOverlay)base.FindName("SmileyView");
		}
	}
}
